package com.yike.user.controller;

import com.yike.user.param.AddMerchantParam;
import com.yike.user.param.SetMerchantParam;
import com.yike.user.param.UpdateMerchantParam;
import com.yike.user.service.MerchantService;
import com.yike.user.vo.MerchantVo;
import lombok.extern.slf4j.Slf4j;
import com.yike.user.param.MerchantPageParam;
import com.yike.common.base.BaseController;
import com.yike.common.api.ApiResult;
import com.yike.common.pagination.Paging;
import com.yike.common.log.Module;
import com.yike.common.log.OperationLog;
import com.yike.common.enums.OperationLogType;
import com.yike.common.api.Update;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 商户表 控制器
 *
 * @author Sakura
 * @since 2024-09-03
 */
@Slf4j
@RestController
@RequestMapping("/merchant")
@Module("user")
@Api(value = "商户管理API", tags = {"商户管理"})
public class MerchantController extends BaseController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 添加商户表
     */
    @PostMapping("/add")
    @OperationLog(name = "添加商户", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商户", response = ApiResult.class)
    public ApiResult<Boolean> addMerchant(@Validated @RequestBody AddMerchantParam addMerchantParam) throws Exception {
        boolean flag = merchantService.saveMerchant(addMerchantParam);
        return ApiResult.result(flag);
    }

    /**
     * 设置商户
     */
    @PostMapping("/setMerchant")
    @OperationLog(name = "设置商户", type = OperationLogType.ADD)
    @ApiOperation(value = "设置商户", response = ApiResult.class)
    public ApiResult<Boolean> setMerchant(@Validated @RequestBody SetMerchantParam setMerchantParam) throws Exception {
        boolean flag = merchantService.setMerchant(setMerchantParam);
        return ApiResult.result(flag);
    }

    /**
     * 修改商户表
     */
    @PostMapping("/update")
    @OperationLog(name = "修改商户", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商户", response = ApiResult.class)
    public ApiResult<Boolean> updateMerchant(@Validated(Update.class) @RequestBody UpdateMerchantParam updateMerchantParam) throws Exception {
        boolean flag = merchantService.updateMerchant(updateMerchantParam);
        return ApiResult.result(flag);
    }

    /**
     * 删除商户表
     */
    @PostMapping("/delete/{merchantNo}")
    @OperationLog(name = "删除商户", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商户", response = ApiResult.class)
    public ApiResult<Boolean> deleteMerchant(@PathVariable("merchantNo") String merchantNo) throws Exception {
        boolean flag = merchantService.deleteMerchant(merchantNo);
        return ApiResult.result(flag);
    }

    /**
     * 获取商户表详情
     */
    @GetMapping("/info/{merchantNo}")
    //@OperationLog(name = "商户详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商户详情", response = MerchantVo.class)
    public ApiResult<MerchantVo> getMerchant(@PathVariable("merchantNo") String merchantNo) throws Exception {
        MerchantVo merchantVo = merchantService.getMerchant(merchantNo);
        return ApiResult.ok(merchantVo);
    }

    /**
     * 商户表分页列表
     */
    @PostMapping("/getPageList")
    //@OperationLog(name = "商户分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商户分页列表", response = MerchantVo.class)
    public ApiResult<Paging<MerchantVo>> getMerchantPageList(@Validated @RequestBody MerchantPageParam merchantPageParam) throws Exception {
        Paging<MerchantVo> paging = merchantService.getMerchantPageList(merchantPageParam);
        return ApiResult.ok(paging);
    }

}

